<template>
  <div>
    <TopBack title="搜一搜"></TopBack>
    <div class="top">
      <van-row class="xiegang">
        <van-col span="6">
          <van-field
            readonly
            required
            clickablelabel="选择地区"
            :value="valueArea"
            placeholder="地区选择"
            @click="bindShow"
          />
          <!--选择省市区-->
          <van-popup
            v-model="showArea"
            position="bottom"
            :style="{ height: '40%' }"
          >
            <van-area
              :area-list="areaList"
              @confirm="onAreaConfirm"
              @cancel="bindCancel"
            />
          </van-popup>
        </van-col>
        <van-col span="8">
          <img class="fengexian" src="../../assets/image/分割线.png" />
          <van-row class="thison2">
            <van-col
              span="5.5"
              class="thison"
             >日期:</van-col
            >
            <van-col span="8.5">
              <van-cell
                :value="start"
                @click="show = true"
                 class="thison"
              />
              <van-calendar v-model="show" @confirm="startDate"  :min-date="minDate"
                  :max-date="maxDate" />
            </van-col>
            <van-col span="1.5" class="thison"
              >-</van-col
            >
            <van-col span="8.5">
              <van-cell
                :value="end"
                @click="show1 = true"
                class="thison"
              />
              <van-calendar v-model="show1" @confirm="endDate"  :min-date="minDate"
                  :max-date="maxDate" />
            </van-col>
          </van-row>
        </van-col>
        <van-col span="10">
          <img class="fengexian" src="../../assets/image/分割线.png" />
          <van-search
            v-model="search"
            class="thison1"
            placeholder="地名/酒店/关键字"
          />
        </van-col>
      </van-row>
    </div>
    <div
      class="shaixuan"
     
    >
      <van-dropdown-menu>
        <van-dropdown-item v-model="value1" :options="option1" />
        <van-dropdown-item v-model="value2" :options="option2" />
      </van-dropdown-menu>
    </div>
    <div class="middle">
      <div v-for="(item, index) in hotel" :key="index">
        <van-card
          :desc="item.hotelType"
          :title="item.name"
          :thumb="item.indexImage"
          tag="双旦人气酒店"
          @click="hotelRoom(item.hotelId)"
        >
          <template #tags>
            <van-tag plain type="danger" class="fenshu">{{ item.num }}</van-tag>
            <van-tag plain type="danger" class="fenshuall">{{
              item.desc
            }}</van-tag>
            <van-tag plain type="danger" class="fenshuall"
              >{{ item.pinglunCount }}+消费</van-tag
            >
          </template>
          <template #price>
            <span>{{ item.adress }}</span>
          </template>
          <template #footer>
            <span class="fubiao"
              >￥{{ item.minPrice }} </span
            >起
          </template>
        </van-card>
      </div>
    </div>
  </div>
</template>

<script>
import TopBack from "../../components/TopBack.vue";
import { areaList } from "@vant/area-data";
import { getAllProductList } from "../../api/hotelApi.js";
export default {
  data() {
    return {
      areaList, //引用地区信息
      valueArea: "",
      showArea: false,
      date: "",
      show: false,
      // 开始日期
      start: "",
      //   结束日期
      end: "",
      show: false,
      show1: false,
      //  存储开始结束日期
      date: {
        start: "",
        end: "",
      },
      search: "", // 搜索内容
      hotel: [], //酒店信息
      value1: 0,
      value2: "a",
      option1: [
        { text: "智能排序", value: 0 },
        { text: "好评优先", value: 1 },
        { text: "点评数优先", value: 2 },
      ],
      option2: [
        { text: "价格", value: "a" },
        { text: "星级", value: "b" },
        { text: "距离", value: "c" },
      ],
      sum: "",
       minDate: new Date(),
      maxDate: new Date(),
    };
  },
  watch:{
    date:{
      deep:true,
      handler(newvalue,oldvalue){
        // console.log(newvalue,'menu',oldvalue)
        // console.log(this.$route.query.start==newvalue)
        // console.log(this.$route.query.start==oldvalue)
        this.dateComp
      }
    }
  },
  methods: {
    bindShow() {
      this.showArea = true;
    },
    bindCancel() {
      this.showArea = false;
    },
    //地区选择
    onAreaConfirm(val) {
      this.showArea = false;
      this.arrArea = val;
      var addrInfo = val[0].name + "-" + val[1].name + "-" + val[2].name;
      this.valueArea = addrInfo;
      this.valueArea = this.valueArea.substr(
        this.valueArea.lastIndexOf("-") + 1
      );
    },
    // 日期选择
    formatDate(date) {
      // console.log(date);
       this.date.start = date;
      return `${date.getMonth() + 1}/${date.getDate()}`;
    },
    formatDate1(date) {
       this.date.end = date;
      return `${date.getMonth() + 1}/${date.getDate()}`;
    },
    startDate(date) {
      this.show = false;
      this.start = this.formatDate(date);
      //  this.dateComp
    },
    endDate(date) {
      this.show1 = false;
      this.end = this.formatDate1(date);
      //  this.dateComp
    },
    // 酒店详情
    hotelRoom(index) {
      // console.log(index);
      // console.log(this.dateComp);
      this.$router.push({ path: "/room", query: { id: index, sum: this.sum,start:this.start,end:this.end } });
      // console.log(this.sum)

    },
  },
  created() {
  this.maxDate = new Date(this.minDate);
    this.maxDate.setDate(this.minDate.getDate() + 30);
    this.start = this.$route.query.start;
    this.end = this.$route.query.end;
    // console.log(this.$route.query,'route.query')
    // console.log(this.$route.query.start)
    // console.log(this.start,'---',this.end)
    if (this.start == "" && this.end == "") {
      var aData = new Date();
      this.value = aData.getMonth() + 1 + "/" + aData.getDate();
      this.start = this.value;
      this.end = this.value;
    }
    this.valueArea = this.$route.query.valueArea;
    this.valueArea = this.valueArea.substr(this.valueArea.lastIndexOf("-") + 1);
    this.sum = this.$route.query.sum;
    if (this.sum == "") {
      this.sum = this.dateComp;
    }
  },
  components: {
    TopBack,
  },
  mounted() {
    getAllProductList().then((res) => {
      this.hotel = res.data;
    });
    // console.log( this.hotel);
  },
   computed: {
    dateComp() {
      this.sum = parseInt(
        (new Date(this.end).getTime() - new Date(this.start).getTime()) / 86400000 
      );
      
      // console.log(this.end,'@@@@',this.start)
      // console.log(this.sum)
      return this.sum;
    },
  },
};
</script>
<style lang="less" scoped>
.top {
  background-color: #f5f6f8;
  left: 3%;
  right: 3%;
  height: 6%;
  padding: 5px 2%;
  margin: 10px;
  height: 20px;
  line-height: 20px;
}

.van-cell {
  padding: 0;
  background-color: #f5f6f8;
  //   height: 20px;
}

.van-cell--required::before {
  content: "";
}
.van-search {
  padding: 0;
  margin-top: 2px;
}
.fengexian {
  width: 2px;
  height: 10px;
}
.van-card__title {
  font-size: 16px;
  font-weight: 600;
}
.fenshu {
  background-color: #033e98;
  border: 0;
  color: #fff;
  border-radius: 5px 0 5px 5px;
  margin-right: 5px;
}
.fenshuall {
  border: 1px solid #fff;
  margin-right: 5px;
  color: #033e98;
}
.van-card__tag {
  position: absolute;

  left: 0;
  top: 1.8rem;
}
.van-card__desc {
  margin-top: 5px;
}
.van-tag {
  margin-top: 5px;
}
.fubiao{
  font-size: 16px; color: red
}
.thison{
font-size: 13px; margin-top: 5px
}
.thison1{
margin-left: 2px; margin-top: -25px
}
.thison2{
  margin-left: 8px; margin-top: -25px
}
.shaixuan{
  margin-top: 10px; border-bottom: 1px solid #ccc
}
</style>
